package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.AnnounceDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.AnnounceEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.AnnounceItemVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
public class AnnounceManager extends BaseManager {
    private final AnnounceDAO announceDAO;

    public AnnounceManager(AnnounceDAO announceDAO) {
        this.announceDAO = announceDAO;
    }

    public Integer getPageCount(String name) {
        int count = announceDAO.count(name);
        return calcPageCount(count, AnnounceDAO.PAGE_SIZE);
    }

    @Transactional
    public List<AnnounceItemVO> getPage(Integer index, String namePart) {
        List<AnnounceItemVO> announceItemList = new ArrayList<>();
        List<AnnounceEntity> announceList = announceDAO.getPage(index, namePart);

        for (AnnounceEntity announce : announceList) {
            int id = announce.getId();
            String name = announce.getName();
            String remarks=announce.getRemarks();

            announceItemList.add(new AnnounceItemVO(id, name,remarks));
        }

        return announceItemList;
    }

    public AnnounceEntity get(Integer id) {
        return announceDAO.get(id);
    }

    public int create(AnnounceEntity entity) {
        return announceDAO.insert(entity);
    }

    public int update(AnnounceEntity entity) {
        return announceDAO.update(entity);
    }

    public int delete(Integer id) {
        return announceDAO.delete(id);
    }

//    public boolean hasMajor(Integer id) {
//        return majorDAO.countByClassRoomId(id) > 0;
//    }
//
//    public boolean hasTeacher(Integer id) {
//        return teacherDAO.countByClassRoomId(id) > 0;
//    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<AnnounceEntity> entityList = announceDAO.listName();
        for (AnnounceEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName(),entity.getRemarks()));
        }

        return voList;
    }
}
